// 引入配置
import {
  baseUrl,
  yxStepKey,
  srStepKey,
  mcStepKey,
} from '@/common/config'
const YX_INDEX_STEP = yxStepKey.step1
const SR_INDEX_STEP = srStepKey.index
const MC_INDEX_STEP = mcStepKey.index
const MC_INDEX_STEP1 = mcStepKey.step1
const baseStepList = [{
    title: '集资参与人信息'
  },
  {
    title: '补充信息'
  },
  {
    title: '信息确认'
  },
]
const extraStepList = [{
    title: '集资参与人信息'
  },
  {
    title: '继承权利人信息'
  },
  {
    title: '补充信息'
  },
  {
    title: '信息确认'
  },
]
export default {
  data() {
    return {
      idCard: '',
      // 是否为集资参与人本人
      oneself: true,
      // 人员类型  1白名单 2已出局人员  3已返款  4剩余全部
      personType: '1',
    }
  },
  computed: {
    stepList() {
      return this.oneself ? baseStepList : extraStepList
    },
    srStepList() {
      return this.oneself ? baseStepList : extraStepList
    }
  },
  methods: {
    // 百度成功回显页面
    baiduSuccessPage(path, idCard, check) {
      return encodeURIComponent(`${baseUrl}${path}?idCard=${idCard}&check=${check}`);
    },
    // 数据回显
    dataBackShow(options, indexKey, step1Key, getInfo, toIndex) {
      const {
        idCard
      } = options;
      this.idCard = idCard || '';

      const info = getInfo(idCard);
      const form = info[this.currentStep] || {
        idCardNo: idCard
      }
      const step1Form = info[step1Key] || {}
      console.log("step1Form: " + JSON.stringify(step1Form.oneself));
      const oneself = step1Form.oneself
      this.oneself = (oneself !== null && oneself !== void 0) ? oneself : true
      this.form = {
        ...this.form,
        ...form
      }
      if (indexKey !== this.currentStep && !info.step) {
        toIndex()
      }
    },
    // 图片回显
    imageBackShow(nameList) {
      nameList.forEach(name => {
        if (this.form[name]) {
          this[name] = [{
            url: this.form[name]
          }];
        }
      })
    },
    // 多个图片回显
    mutiImageBackShow(nameList) {
      nameList.forEach(name => {
        if (this.form[name]) {
          this[name] = this.form[name].map(item => {
            return {
              url: item
            }
          });
        }
      })
    },
    // 上传单个文件写入form
    atterUploadFile(fileList, name) {
      this.form[name] = fileList[0].url;
      this.$refs.form.validateField(name);
      uni.hideLoading();
    },
    // 删除单个文件删除form
    atterDeleteFile(fileList, name) {
      this.form[name] = '';
      this.$refs.form.validateField(name);
    },
    // 上传多个文件写入form
    atterUploadMutiFile(fileList, name) {
      this.form[name] = fileList.map(item => item.url);
      this.$refs.form.validateField(name);
       uni.hideLoading();
    },
    // 删除多个文件删除form
    atterDeleteMutiFile(fileList, name) {
      this.form[name] = fileList.map(item => item.url);
      this.$refs.form.validateField(name);
    },
    // 删除图片
    deleteFile(event, deleteFileAfter) {
      this[`${event.name}`].splice(event.index, 1)
      deleteFileAfter(this[`${event.name}`], event.name)
    },
    // 上传图片
    async uploadFile(event, uploadFileAfter) {
      uni.showLoading({
        title: '图片上传中，请稍后...',
      })
      let lists = [].concat(event.file)
      let fileListLen = this[`${event.name}`].length
      lists.map((item) => {
        this[`${event.name}`].push({
          ...item,
          status: 'uploading',
          message: '上传中'
        })
      })
      for (let i = 0; i < lists.length; i++) {
        const result = await this.uploadFilePromise(lists[i].url)
        let item = this[`${event.name}`][fileListLen]
        this[`${event.name}`].splice(fileListLen, 1, Object.assign(item, {
          status: 'success',
          message: '',
          url: result
        }))
        fileListLen++
      }
      uploadFileAfter(this[`${event.name}`], event.name)
    },
    // 上传图片
    uploadFilePromise(url) {
      return new Promise((resolve, reject) => {
        let a = uni.uploadFile({
          url: '/api/common/anonymous/upload', // 仅为示例，非真实的接口地址
          filePath: url,
          name: 'file',
          success: (res) => {
            resolve(JSON.parse(res.data).fileName)
          }
        });
      })
    },
    // 构建页面证件号参数
    buildParams(idCard) {
      return '?idCard=' + idCard;
    },
	// 构建页面证件号参数
	buildParamsAndName(idCard,name) {
	  return '?idCard=' + idCard+'&name='+name;
	},
    baseSaveInfo(idCard, step, getInfo, saveInfo) {
      // 获取存储信息
      const info = getInfo(idCard);
      let data = {
        ...info,
        step: step,
        idCard
      }
      data[this.currentStep] = this.form;
      saveInfo(idCard, data);
    },
    // 映霞案主页
    toYxIndex() {
      uni.reLaunch({
        url: this.$yxStepPath[YX_INDEX_STEP]
      })
    },
    toYxResult(state,reason,idCard) {
      uni.reLaunch({
        url: `${this.$yxStepPath[yxStepKey.result]}?state=${state}&reason=${reason}&idCard=${idCard}`
      })
    },
    // 映霞案上一页
    toPreYxPage() {
      // 获取存储信息
      this.baseSaveInfo(this.idCard, this.preStep, this.$getYxInfo, this.$setYxInfo);
      uni.reLaunch({
        url: this.$yxStepPath[this.preStep] + this.buildParams(this.idCard)
      })
    },
    toNextYxPage(idCard){
      this.saveYxInfo(idCard);
      uni.reLaunch({
        url: this.$yxStepPath[this.nextStep] + this.buildParams(idCard)
      })
    },
    // 存储映霞案信息
    saveYxInfo(idCard) {
      this.baseSaveInfo(idCard, this.nextStep, this.$getYxInfo, this.$setYxInfo);
    },
    // 映霞案人脸识别后返回地址
    getYxBaiduSuccessPage() {
      return this.baiduSuccessPage(this.$yxStepPath[yxStepKey.step3], this.idCard, true)
    },
    // 校验映霞案页面
    checkYxPage(options) {
      this.dataBackShow(options, YX_INDEX_STEP, yxStepKey.step1, this.$getYxInfo, this.toYxIndex)
      // const {
      //   idCard
      // } = options;
      // this.idCard = idCard || '';

      // const yxInfo = this.$getYxInfo(idCard);
      // const form = yxInfo[this.currentStep] || {}
      // const step1Form = yxInfo[YX_INDEX_STEP] || {}
      // const oneself = step1Form.oneself
      // this.oneself = (oneself !== null && oneself !== void 0) ? oneself : true
      // this.form = {
      //   ...this.form,
      //   ...form
      // }


      // if (YX_INDEX_STEP !== this.currentStep && !yxInfo.step) {
      //   this.toYxIndex()
      // }
    },
    // 圣瑞案
    toSrIndex() {
      uni.reLaunch({
        url: this.$srStepPath[SR_INDEX_STEP]
      })
    },
    toSrResult(state,reason,idCard,cid) {
      uni.reLaunch({
        url: `${this.$srStepPath[srStepKey.result]}?state=${state}&reason=${reason}&idCard=${idCard}&cid=${cid}`
      })
    },
    toSrRstart(){
      uni.reLaunch({
        url: this.$srStepPath[SR_INDEX_STEP] + this.buildParams(this.idCard)
      })
    },
    // 圣瑞案上一页
    toPreSrPage() {
      // 获取存储信息
      this.baseSaveInfo(this.idCard, this.preStep, this.$getSrInfo, this.$setSrInfo);
      uni.reLaunch({
        url: this.$srStepPath[this.preStep] + this.buildParams(this.idCard)
      })
    },
    toNextSrPage(idCard){
      this.saveSrInfo(idCard)
      uni.reLaunch({
        url: this.$srStepPath[this.nextStep] + this.buildParams(idCard)
      })
    },
    // 存储圣瑞案信息
    saveSrInfo(idCard) {
      this.baseSaveInfo(idCard, this.nextStep, this.$getSrInfo, this.$setSrInfo);
    },
    // 圣瑞案人脸识别后返回地址
    getSrBaiduSuccessPage(check) {
      return this.baiduSuccessPage(this.$srStepPath[srStepKey.step3], this.idCard, check)
    },
    // 校验圣瑞案页面，回显数据
    checkSrPage(options) {
      this.dataBackShow(options, SR_INDEX_STEP, srStepKey.step1, this.$getSrInfo, this.toSrIndex)
    },
	// 秒诚案
	toMcIndex() {
	  uni.reLaunch({
	    url: this.$mcStepPath[MC_INDEX_STEP]
	  })
	},
    toMcResult(state,reason,idCard,cid) {
      uni.reLaunch({
        url: `${this.$mcStepPath[mcStepKey.result]}?state=${state}&reason=${reason}&idCard=${idCard}&cid=${cid}`
      })
    },
    toMcRstart(name){
      uni.reLaunch({
        url: this.$mcStepPath[MC_INDEX_STEP] + this.buildParamsAndName(this.idCard,name)
      })
    },
    // 秒诚案上一页
    toPreMcPage() {
      // 获取存储信息
      this.baseSaveInfo(this.idCard, this.preStep, this.$getSrInfo, this.$setSrInfo);
      uni.reLaunch({
        url: this.$mcStepPath[this.preStep] + this.buildParams(this.idCard)
      })
    },
    toNextMcPage(idCard){
      this.saveMcInfo(idCard)
      uni.reLaunch({
        url: this.$mcStepPath[this.nextStep] + this.buildParams(idCard)
      })
    },
    // 存储秒城案信息
    saveMcInfo(idCard) {
      this.baseSaveInfo(idCard, this.nextStep, this.$getMcInfo, this.$setMcInfo);
    },
    // 秒城案人脸识别后返回地址
    getMcBaiduSuccessPage(check) {
      return this.baiduSuccessPage(this.$mcStepPath[mcStepKey.step3], this.idCard, check)
    },
    // 校验圣瑞案页面，回显数据
    checkMcPage(options) {
      this.dataBackShow(options, MC_INDEX_STEP, mcStepKey.step1, this.$getMcInfo, this.toMcIndex)
    }
  }
}
